﻿using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MVCGenerator
{
    /// <summary>
    /// My Favorite Extensions 
    /// </summary>
    public static class Extensions
    {
        public static bool IsIn<T>(this T source, params T[] list)
        {
            if (null == source)
            {
                throw new ArgumentNullException("Parameter Source is null");
            }
            return list.Contains(source);
        }

        public static Boolean IsDate(this object Expression)
        {
            Boolean IsDateResult = false;
            if (Expression == null)
            {
                // do nothing
            }
            else
            {
                DateTime Result = DateTime.MaxValue;
                if (DateTime.TryParse(Expression.ToString(), out Result))
                {
                    IsDateResult = true;
                }
                else
                {
                    // do nothing
                }
            }
            return IsDateResult;
        }

        public static Boolean IsLong(this object Expression)
        {
            Boolean IsDateResult = false;
            if (Expression == null)
            {
                // do nothing
            }
            else
            {
                long Result = long.MaxValue;
                if (long.TryParse(Expression.ToString(), out Result))
                {
                    IsDateResult = true;
                }
                else
                {
                    // do nothing
                }
            }
            return IsDateResult;
        }

        /// <summary>
        /// MUST Be a datetime
        /// </summary>
        /// <param name="Expression">String Date Representation</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this String Expression)
        {
            DateTime ResultToDate;
            DateTime Result;
            if (DateTime.TryParse(Expression, out Result))
            {
                ResultToDate = Result;
            }
            else
            {
                // do nothing
            }
            return Result;
        }

        public static String ToLoggableString(this OdbcCommand Command)
        {
            StringBuilder Message = new StringBuilder();
            Message.AppendLine("OdbcCommand: " + Command.CommandText);

            foreach (OdbcParameter Parameter in Command.Parameters)
            {
                Message.AppendLine("[" + Parameter.ParameterName + "] [" + Parameter.Value.ToString() + "]");
            }

            return Message.ToString();
        }

        public static String ToLoggableString(this SqlCommand Command)
        {
            StringBuilder Message = new StringBuilder();
            Message.AppendLine("SqlCommand: " + Command.CommandText);

            foreach (SqlParameter Parameter in Command.Parameters)
            {
                Message.AppendLine("[" + Parameter.ParameterName + "] [" + Parameter.Value.ToString() + "]");
            }

            return Message.ToString();
        }

        /// <summary>
        /// MUST Be a long
        /// </summary>
        /// <param name="Expression">long Representation</param>
        /// <returns></returns>
        public static long ToLong(this String Expression)
        {
            long Result;
            if (long.TryParse(Expression, out Result))
            {
                // Success
            }
            else
            {
                // do nothing
            }
            return Result;
        }

        /// <summary>
        /// MUST Be a long
        /// </summary>
        /// <param name="Expression">long Representation</param>
        /// <returns></returns>
        public static long ToLong(this Object Expression)
        {
            long Result;
            if (long.TryParse(Expression.ToString(), out Result))
            {
                // Success
            }
            else
            {
                // do nothing
            }
            return Result;
        }

        /// <summary>
        /// MUST Be a bool
        /// </summary>
        /// <param name="Expression">bool Representation</param>
        /// <returns></returns>
        public static bool ToBoolean(this Object Expression)
        {
            bool Result = false;

            if (Expression == null)
            {

            }
            else
            {
                if (bool.TryParse(Expression.ToString(), out Result))
                {
                    // Success
                }
                else
                {
                    // do nothing
                }
              
            }  
            return Result;
        }

        /// <summary>
        /// MUST Be a int
        /// </summary>
        /// <param name="Expression">int Representation</param>
        /// <returns></returns>
        public static int ToInt(this Object Expression)
        {
            int Result;
            if (int.TryParse(Expression.ToString(), out Result))
            {
                // Success
            }
            else
            {
                // do nothing
            }
            return Result;
        }

        /// <summary>
        /// MUST Be a DateTime
        /// </summary>
        /// <param name="Expression">DateTime Representation</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this Object Expression)
        {
            DateTime Result;
            if (DateTime.TryParse(Expression.ToString(), out Result))
            {
                // Success
            }
            else
            {
                // do nothing
            }
            return Result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="o">A variable of type Object</param>
        /// <returns>
        /// True if the parameter o is null
        /// False if the parameter o is not null
        /// </returns>
        [System.Diagnostics.DebuggerStepThrough()]
        public static bool IsNull(this object o)
        {
            return (o == null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="o">A variable of type object</param>
        /// <returns>
        /// True if the parameter o is null
        /// False if the parameter o is not null
        /// </returns>
        /// 
        [System.Diagnostics.DebuggerStepThrough()]
        public static bool IsNotNull(this String Expression)
        {
            if (Expression == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        [System.Diagnostics.DebuggerStepThrough()]
        public static bool IsNotNull(this Object Expression)
        {
            if (Expression == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
